#include "room.h"

Room::Room(QString ownerIdentify, QString roomName) {
    this->ownerIdentify = ownerIdentify;
    this->name = roomName;
    this->needPass = false;
}

QString Room::getName() {
    return this->name;
}

QString Room::getOwnerIdentify() {
    return this->ownerIdentify;
}

void Room::setOwnerIdentify(QString identify) {
    this->ownerIdentify = identify;
}

bool Room::getPassNeed() {
    return this->needPass;
}

QString Room::getPassHash() {
    return this->passHash;
}

void Room::setPass(QString passHash) {
    this->needPass = !passHash.isEmpty();
    this->passHash = passHash;
}

QString Room::getDescription() {
    return this->description;
}

void Room::setDescription(QString description) {
    this->description = description;
}

QList<User*> Room::getModerators() {
    return this->moderators;
}

void Room::addModerator(User *user) {
    if (!this->moderators.contains(user)) {
        this->moderators.append(user);
    }
}

void Room::removeModerator(User *user) {
    if (this->moderators.contains(user)) {
        this->moderators.removeAt( this->moderators.indexOf(user) );
    }
}

QList<User*> Room::getUsers() {
    return this->users;
}

void Room::addUser(User *user) {
    if (!this->users.contains(user)) {
        this->users.append(user);
    }
}

void Room::removeUser(User *user) {
    if (this->users.contains(user)) {
        this->users.removeAt( this->users.indexOf(user) );
    }
}

bool Room::isModerator(User *user) {
    for (int i = 0; i < this->moderators.size(); i++) {
        if (user->getIdentify().compare( this->moderators.at(i)->getIdentify() ) == 0) {
            return true;
        }
    }
    return false;
}

bool Room::isOwner(User *user) {
    return user->getIdentify().compare( this->ownerIdentify ) == 0;
}

bool Room::isMember(User *user) {
    return this->users.contains(user);
}

void Room::addMessage(Message *message) {
    this->messages.append(message);
}

QList<Message*> Room::getMessages(int limit) {
    // @todo limit messages
    return this->messages;
}
